<?php
namespace common\logics\regqrcode;

use common\widgets\Tool;
use yii\base\BaseObject;
use Yii;

class RegQrCodeLogic extends BaseObject implements IRegQrCode
{
    /** @var \common\sources\read\regqrcode\RegQrCodeAccess $RRegQrCodeAccess ***/ //依赖注入模式
    private $RRegQrCodeAccess;//存储读数据对象
    /** @var \common\sources\write\regqrcode\RegQrCodeAccess $WRegQrCodeAccess */
    private $WRegQrCodeAccess;

    public function init()
    {
        $this->RRegQrCodeAccess = Yii::$container->get("RRegQrCodeAccess");//获取读数据对象
        $this->WRegQrCodeAccess = Yii::$container->get("WRegQrCodeAccess");//获取写数据对象
        parent::init(); // TODO: Change the autogenerated stub
    }

    /**
     * @param $data 参数数组
     * page_num 页数
     */
    public function getRegQrCodeList() {
        $qrCodeList = $this->RRegQrCodeAccess->getRegQrCodeList();
        if (!$qrCodeList) {//为空
            return Tool::returnJson([
                'list' => []
            ]);
        }
        return Tool::returnJson([
            'list' => $qrCodeList,
        ],200,"成功");
    }

    /**
     * @param $param
     * 设置注册二维码图片
     */
    public function setRegQrCodeImg($data) {
        if (empty($data['qrcode_id'])) {
            return Tool::returnJson([],401,"二维码id不能为空");
        }
        if (empty($data['qrcode_img'])) {
            return Tool::returnJson([],401,"二维码图片不能为空");
        }
        $result = $this->WRegQrCodeAccess->setRegQrCodeImg($data);
        if (!$result) {
            return Tool::returnJson([],409,"修改失败");
        }
        return Tool::returnJson([],200,"修改成功");
    }

    /**
     * 启用二维码
     */
    public function UseQrCode($data) {
        if (empty($data['qrcode_id'])) {
            return Tool::returnJson([],401,"二维码id不能为空");
        }
        if (!isset($data['is_use'])) {
            $data['is_use'] = 1;//启用该二维码
        }

        $result = $this->WRegQrCodeAccess->UseQrCode($data);
        if (!$result) {
            return Tool::returnJson([],409,"设置失败");
        }
        return Tool::returnJson([],200,"设置成功");
    }

    /**
     * 删除注册二维码
     */
    public function DelQrCode($data) {
        if (empty($data['qrcode_id'])) {
            return Tool::returnJson([],401,"二维码id不能为空");
        }
        $result = $this->WRegQrCodeAccess->DelQrCode($data);
        if (!$result) {
            return Tool::returnJson([],409,"删除失败");
        }
        return Tool::returnJson([],200,"删除成功");
    }

    /**
     * @param $data
     * 新增注册二维码
     */
    public function InsQrCode($data) {
        if (empty($data['qrcode_name'])) {
            return Tool::returnJson([],401,"二维码名称不能为空");
        }
        if (empty($data['qrcode_img'])) {
            return Tool::returnJson([],401,"二维码图片不能为空");
        }
        $result = $this->WRegQrCodeAccess->InsQrCode($data);
        if (!$result) {
            return Tool::returnJson([],409,"新增失败");
        }
        return Tool::returnJson([],200,"新增成功");
    }
}